<?php

class Materi extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('admin/m_materi');
        $this->load->library('pagination');
		$this->load->helper('tgl_indonesia');
	}
	
	function index(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_materi->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/materi/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$data['tabel'] = $this->m_materi->getTable($config['per_page'], $this->uri->segment(4));
			$data['main_view'] = 'admin/materi/index';
			$this->load->view('admin/template', $data);
		}
	}
	
	function add(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$data['divisi'] = $this->m_materi->getAllDivisi();
			$data['main_view'] = 'admin/materi/add';
			$this->load->view('admin/template', $data);
		}
	}
	
	function insert(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('judul', 'Judul', 'required');
			$this->form_validation->set_rules('deskripsi', 'Deskripsi', 'required');
			$this->form_validation->set_rules('divisi', 'Divisi', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			if($this->form_validation->run()==true){
				$judul = $this->input->post('judul');
				$deskripsi = $this->input->post('deskripsi');
				$tgl = date("Y-m-d");
				$hari = nama_hari($tgl);
				$seo = str_replace(" ", "-", $judul);
				$judul_seo = strtolower($seo . ".html");
				$div = $this->input->post('divisi');
				$file_asli=$_FILES['userfile']['name'];
				$file = str_replace(" ", "_", $file_asli);
				$acak=rand(00000000000,99999999999);
				$file_bru = $acak.$file;
				$file_default = "null";
				
				if(!empty($file_asli)){
					$config['upload_path'] = './image/materi/';
					$config['allowed_types'] = 'pdf|rar|zip|doc';
					$config['max_size'] = '20000';
					$config['file_name']= $file_bru;				
					$this->load->library('upload', $config);
				
					if(!$this->upload->do_upload())
					{
						echo $this->upload->display_errors();
					}else{
						$this->m_materi->create($file_bru,$judul,$judul_seo, $deskripsi,1,$div);
						redirect('admin/materi');
					}
				}else{
					$this->m_materi->create($file_default,$judul,$judul_seo, $deskripsi,1,$div);
					redirect('admin/berita');
				}
			}else{
				$data['divisi'] = $this->m_materi->getAllDivisi();
				$data['main_view'] = 'admin/materi/add';
				$this->load->view('admin/template', $data);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function edit(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			//$id = $this->uri->segment();
			$data['data'] = $this->m_materi->getDataById($this->uri->segment(4));
			$data['divisi'] = $this->m_materi->getAllDivisi();
			$data['main_view'] = 'admin/materi/edit';
			$this->load->view('admin/template', $data);
		}
	}
	
	function update($id){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('judul', 'Judul', 'required');
			$this->form_validation->set_rules('deskripsi', 'Deskripsi', 'required');
			$this->form_validation->set_rules('divisi', 'Divisi', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			$id = $this->input->post('id_materi');
			if($this->form_validation->run()==true){
				$judul = $this->input->post('judul');
				$deskripsi = $this->input->post('deskripsi');
				$tgl = date("Y-m-d");
				$hari = nama_hari($tgl);
				$seo = str_replace(" ", "-", $judul);
				$judul_seo = strtolower($seo . ".html");
				$div = $this->input->post('divisi');
				$file_asli=$_FILES['userfile']['name'];
				$file_lama = $this->input->post('filelama');
				$file = str_replace(" ", "_", $file_asli);
				$acak=rand(00000000000,99999999999);
				$file_bru = $acak.$file;
				//$file_default = "null";
				
				if(!empty($file_asli)){
					$config['upload_path'] = './image/materi/';
					$config['allowed_types'] = 'pdf|rar|zip|doc';
					$config['max_size'] = '20000';
					$config['file_name']= $file_bru;				
					$this->load->library('upload', $config);
				
					if(!$this->upload->do_upload())
					{
						echo $this->upload->display_errors();
					}else{
						$file = './image/materi/' . $file_lama;
						unlink($file);
						$id = $this->input->post('id_materi');
						$this->m_materi->update($id,$file_bru,$judul,$judul_seo, $deskripsi,$div);
						redirect('admin/materi');
					}
				}else{
					$id = $this->input->post('id_materi');
					$this->m_materi->update($id,$file_lama,$judul,$judul_seo, $deskripsi,$div);
					redirect('admin/materi');
				}
			}else{
				
				redirect('admin/materi/edit/' . $id);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function delete(){
		$id = $this->uri->segment(4);
		$gbr= $this->uri->segment(5);
		$file = './image/materi/'.$gbr;
		unlink($file);
		$this->m_materi->delete($id);
		redirect('admin/materi');
	}
	
	function search(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_materi->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/materi/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$judul = $this->input->post('judul');
			
			$data['tabel'] = $this->m_materi->search($judul);
			$data['main_view'] = 'admin/materi/index';
			$this->load->view('admin/template', $data);
		}	
	}
	
}